#include <wiiuse/wpad.h>
#include "missilecommand.h"
#include "gametrigger.h"
#include "gameaction.h"

using std::vector;
using std::string;

extern Game *game;

GameActionList::~GameActionList()
{
  vector<GameAction*>::iterator it;
  for(it = actions.begin(); it != actions.end(); ++it)
    delete *it;
}

void GameActionList::begin()
{
  current = actions.begin();
}

bool GameActionList::perform()
{
  while(current != actions.end()) {
    GameAction *curAction = *current;
    if(!curAction->perform())
      return false;
    current++;
  }
  return true;
}

void GameActionList::add(GameAction *action)
{
  actions.push_back(action);
}

void GameActionList::relJump(int jump)
{
  if(jump < 0) {
    for(int i = 0; i > jump && current != actions.begin(); i--)
      current--;
  } else {
    for(int i = 0; i < jump && current != actions.end(); i++)
      current++;
  }
}

ShowTextAction::ShowTextAction(const char *str, f32 x, f32 y)
{
  game->modaltext->set(str, x, y);
}

bool ShowTextAction::perform()
{
  game->enableText = true;
  return true;
}


bool FadeOutTextAction::perform()
{
/*  int curfade = game->textColor.a;
  curfade -= 2;
  if(curfade <= 0) {
    game->enableText = false;
    game->textColor.a=255;
    return true;
  }
  game->textColor.a=curfade;
*/
  return false;
}

bool HideTextAction::perform()
{
  game->enableText = false;
  return true;
}

SetFadeAction::SetFadeAction(f32 fadeLevel)
  : fadeLevel(fadeLevel) {};

bool SetFadeAction::perform()
{
  game->targetfade = fadeLevel;
  game->fade = fadeLevel;
  return true;
}

FadeToAction::FadeToAction(f32 fadeLevel)
  : fadeLevel(fadeLevel) {};

bool FadeToAction::perform()
{
  game->targetfade = fadeLevel;
  if(game->fade == fadeLevel)
    return true;
  return false;
}

LoopAction::LoopAction(int looptarget, LoopActionCondition *loopcondition) {};

bool LoopAction::perform() {return true;}

DelayAction::DelayAction(int delayFrames)
  : delayFrames(delayFrames), frameCount(0) {};

bool DelayAction::perform()
{
  if(frameCount >= delayFrames) {
    frameCount = 0;
    return true;
  }
  frameCount++;
  return false;
}

WaitButtonPressAction::WaitButtonPressAction(u32 buttonMask)
  : buttonMask(buttonMask) {};

bool WaitButtonPressAction::perform() {
  if(WPAD_ButtonsDown(0)&buttonMask)
    return true;
  return false;
}

bool PauseAction::perform()
{
  game->pause = true;
  return true;
}

bool ResumeAction::perform()
{
  game->pause = false;
  return true;
}

bool DisableGameInputAction::perform()
{
  game->enablePlayerControl = false;
  return true;
}

bool EnableGameInputAction::perform()
{
  game->enablePlayerControl = true;
  return true;
}

bool AbortAction::perform() {return true;}

AddMissileAction::AddMissileAction(MissileInfo *minfo) {};

bool AddMissileAction::perform() {return true;}

RegisterTriggerAction::RegisterTriggerAction(GameTrigger *trigger)
  : trigger(trigger) {};

RegisterTriggerAction::~RegisterTriggerAction() {delete trigger;}

bool RegisterTriggerAction::perform()
{
  game->triggers.push_back(trigger);
  return true;
}

LoadActionListAction::LoadActionListAction(GameActionList *actionlist)
  : actionlist(actionlist) {};

bool LoadActionListAction::perform()
{
  game->al = actionlist;
  game->al->begin();
  // false so we don't continue with the current actionlist
  return false;
}

bool ReInitializeAction::perform()
{
  game->initialize();
  game->activate();
  return true;
}
